<template>
	<view class="zone">
		<Header title="我的宝石" color="#fff"></Header>
		<view class="main">
			<view class="top">
				<view class="t_top">
					<view class="left">
						<p>剩余宝石</p>
						<h3>{{userinfo.jewel}}</h3>
					</view>
					<image src="http://img.cpgm.cc/panda/static/self/baos.png" mode="heightFix" />
				</view>
				<p class="source">宝石来源：目前只有在潮玩西游可以挖到</p>
			</view>

				<scroll-view scroll-y class="scrollbox"  @scrolltolower="scrolltolower" >
					<view class="lsit">
						<view class="item" v-for="(item, index) in datalist" :key="index">
							<view class="left">
								<p>{{item.remark}}</p>
								<p>{{item.created_at}}</p>
							</view>
							<view class="right">{{item.operation_type == 1?'+':'-'}}{{item.number}}</view>
						</view>		
						<view v-if="datalist.length==0" class="empty">
							<image src="http://img.cpgm.cc/panda/static/public/empty.png" class="zaneu_img" mode="widthFix"></image>
							<text>没有数据哦~</text>
						</view>
						<u-loadmore v-else :status="status" :icon-type="iconType" :load-text="loadText" />			
					</view>
				</scroll-view>
			<view class="button exchange" @click="show = true">
				<text class="b_top">兑换银元</text>
				<p>
					<image src="http://img.cpgm.cc/panda/static/self/yb.png" mode="widthFix" />
					<text>1宝石=5银元</text>
				</p>
			</view>
		</view>


		<u-mask :show ="show">
			<view class="playbox" :class="[show?'playboxac':'']">
				<image class="closed" @click="show = false" src="http://img.cpgm.cc/panda/static/public/close.png" mode="widthFix" />
				<view class="content">
					<image class="yb" src="http://img.cpgm.cc/panda/static/self/bigyb.png" mode="widthFix" />
					<h3>兑换银元</h3>
					<p class="explain"> <text>1</text> 宝石= <text>5</text> 银元</p>
					<view class="piece">
						<text>宝石数量</text>
						<input type="number" @input="input" v-model="jewel" >
					</view>
					<view class="piece">
						<text>银元数量</text>
						<input type="number" disabled="disabled" v-model="value2">
					</view>

					<view class="button" @click="jewelToSilver()">立即兑换</view>
				</view>
			</view>
		</u-mask>


		<u-mask :show="show2">
			<view class="warp">
				<view class="maskbox">
					<image class="topimg" src="http://img.cpgm.cc/panda/static/mask/laba.png" mode="widthFix" />
					<view class="wightbg">
						<p>恭喜您成功获得</p>
						<image src="http://img.cpgm.cc/panda/static/self/yb.png" mode="widthFix" />
						<text>{{silver}}银元</text>
					</view>
				</view>
				<view class="mask_btn button" @click="show2 = false">开心收下</view>
			</view>
		</u-mask>



	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:false,
				jewel:'',
				value2:'',
				userinfo: uni.getStorageSync('userinfo'),
				show2:false,
				silver:'',
				page: 1,
				max_page: 1,
				status: 'loadmore',
				iconType: 'flower',
				loadText: {
					loadmore: '上拉加载',
					loading: '努力加载中',
					nomore: '没有更多了'
				},
				datalist: [],
			};
		},

		onLoad(){
			this.getUserInfo()
			this.getJewelList();
		},

		methods: {
			// 请求用户信息接口
			getUserInfo() {
				this.$getUserInfo.getUserInfo((res) => {
					this.userinfo = res.data
					if (res.data.vip_card > 0) {
						this.giveshow = 1
					}
				})
			},

			// 获取银元明细
			async getJewelList() {
				let res = await this.$http.index.getJewelList({
					page: this.page
				})
				this.datalist = [...this.datalist, ...res.data];
				this.max_page = res.page;
				if (this.page >= this.max_page) {
					this.status = 'nomore'
				}
			},

			async getJewelList2() {
				let res = await this.$http.index.getJewelList({
					page: 1
				})
				this.datalist = res.data;
				this.max_page = res.page;
				if (this.page >= this.max_page) {
					this.status = 'nomore'
				}
			},

				scrolltolower() {
				if (this.page >= this.max_page) {
					this.status = 'nomore'
				} else {
					this.status = 'loading';
					this.page = ++this.page;
					this.getJewelList();
					setTimeout(() => {
						if (this.page > this.max_page || this.page == this.max_page) this.status = 'nomore';
						else this.status = 'loading';
					}, 500)
				}
			},

			// 宝石兑换银元
			async jewelToSilver(){
				let res = await this.$http.index.jewelToSilver({
					num:this.jewel
				})
				if(res.code == 1) {
					this.getUserInfo()
					this.getJewelList2();
					this.silver = this.value2
					this.show = false
					this.show2 = true
				}else {
					this.$u.toast(res.msg)
				}
			},

			// inpu框输入后
			input(e) {
				e.target.value = e.target.value.replace(/^0|[^\d]|[.]/g, '')
				this.$nextTick(() => {
					this.jewel =  e.target.value
					if(this.jewel > Number(this.userinfo.jewel)) {
						this.$nextTick(() => {
							this.jewel = Math.trunc(Number(this.userinfo.jewel)) ;
							this.value2 = (this.jewel * 5)
						});
					}else {
						this.value2 = (this.jewel * 5)
					}
				})
			},
		},
	}
</script>

<style lang="less">
.zone {
	background: linear-gradient(180deg, #0477FD 0%, #7AFDCF 100%);
	min-height: 100vh;
}

.main {
	padding: 10px ;
}

.top {
	background: linear-gradient(90deg, #4F4E4C 0%, #242424 100%);
	box-shadow: 0px 5px 8px 0px rgba(0,0,0,0.2), inset -4px 0px 5px 0px rgba(53,53,53,1), inset 0px -4px 5px 0px rgba(77,77,77,1);
	border-radius: 13px;
	padding: 15px;

	.source {
		color: rgba(255, 255, 255, 0.6);
		font-size: 14px;
		font-weight: 600;
		margin-top: 20px;
	}
}

.t_top {
	display: flex;
	align-items: center;
	justify-content: space-between;

	.left {
		display: flex;
		flex-direction: column;

		p {
			color: #FFFFFF;
			font-size: 13px;
			
		}
		h3 {
			color: #FFFFFF;
			font-size: 30px;
			margin-top: 4px;
		}
	}

	image {
		height: 65px;
	}
}

.list {
	padding: 10px 0 50px;
}

.scrollbox {
	height: calc(100vh - 310px);

	.item {
		background: rgba(255, 255, 255, 0.8);
		border-radius: 8px;
		padding: 10px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 8px;

		.left {
			p:nth-child(1) {
				color: #131314;
				font-size: 14px;
				font-weight: 600;
			}
			p:nth-child(2) {
				color: #333333;
				font-size: 12px;
				opacity: 0.8;
				margin-top: 4px;
			}
		}

		.right {
			font-size: 14px;
			font-weight: 600;
			color: #333333;
		}
	}
}



.exchange {
	width: 80%;
	left: 50%;
	transform: translateX(-50%);
	box-sizing: border-box;
	max-width: 400px;
	position: fixed;
	bottom: 30px;
	display: flex;
	flex-direction: column;
	align-items: center;
	background: url('http://img.cpgm.cc/panda/static/public/btn3.png');
	background-size: 100% 100%;
	padding: 10px;

	.b_top {
		color: #B73800;
		font-size: 20px;
		font-weight: 600;
	}

	p {
		display: flex;
		align-items: center;

		image {
			width: 20px;
		}
		text {
			color: #B73800;
			font-size: 14px;
		}
	}
}






.playbox {
	width: 100%;
	position: absolute;
	left: 0%;
	bottom: -50vh;
	background: url('http://img.cpgm.cc/panda/static/mask/bbg.png');
	background-size: 100% 100%;
	transition: all 0.3s linear;

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 20px;

		.yb {
			width: 32%;
			margin-top: -18%;
		}

		h3 {
			position: relative;
			color: #000000;
			font-size: 20px;
			width: 50%;
			text-align: center;
			z-index: 2;
		}

		.explain {
			color: #333333;
			font-size: 14px;
			margin: 10px 0;
			text {
				font-weight: 600;
			}
		}

		h3::after {
			content: '';
			width: 100%;
			height: 30px;
			background: url('http://img.cpgm.cc/panda/static/mask/banner.png');
			background-size: 100% auto;
			background-position: center;
			background-repeat: no-repeat;
			position: absolute;
			top: 0;
			left: 0;
			z-index: -1;
		}

		.piece {
			width: 100%;
			background: #FFFFFF;
			border-radius: 9px;
			padding: 15px;
			display: flex;
			align-items: center;
			margin: 5px 0;

			text {
				color: #1D1E24;
				font-size: 14px;
				font-weight: 600;
				margin-right: 10px;
			}

			input {
				text-align: center;
				flex: 0.9;
			}
		}

		.button {
			height: 68px;
			width: 100%;
			background: url('http://img.cpgm.cc/panda/static/public/btn3.png');
			background-size: 100% 100%;
			margin-top: 30px;
			color: #B73800;
			font-size: 18px;
			font-weight: 600;
		}

	}
	.closed {
		position: absolute;
		width: 25px;
		top: 8%;
		right: 15px;
	}
}

.playboxac {
	bottom:0 !important;
}






// 弹窗

.maskbox {
	background: linear-gradient(180deg, #61FBED 0%, #E4FDFC 31%, #F6FEEB 68%, #F5FEA8 100%);
	box-shadow: inset 5px 5px 47px 0px rgba(255,255,255,0.5), inset -5px -5px 47px 0px rgba(255,255,255,0.5), inset 0px 2px 0px 0px rgba(255,255,255,0.5);
	padding: 15px;
	min-height: 300px;
	border-radius: 10px;
	display: flex;
	flex-direction: column;
	align-items: center;

	.topimg {
		width: 35%;
		margin: 0 auto;
		margin-top: -25%;
	}

	h3 {
		color: #000000;
		font-size: 26px;
	}

	.wightbg {
		width: 100%;
		background: rgba(255, 255, 255, 0.7);
		border-radius: 9px;
		padding: 20px 10px;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 15px;

		p {
			position: relative;
			color: #000000;
			font-size: 20px;
			font-weight: 600;
			z-index: 2;
			padding: 0 10px;
		}

		p::after {
			content: '';
			width: 100%;
			height: 30px;
			background: url('http://img.cpgm.cc/panda/static/mask/banner.png');
			background-size: 100% auto;
			background-position: center;
			background-repeat: no-repeat;
			position: absolute;
			top: 0;
			left: 0;
			z-index: -1;
		}

		image {
			width: 30%;
			margin: 10px 0;
		}
		text {
			color: #000000;
			font-size: 20px;
			font-weight: 600;
		}
	}
}

.mask_btn {
	width: 50%;
	height: 58px;
	background: url('http://img.cpgm.cc/panda/static/public/btn2.png');
	background-size: 100% 100%;
	margin: 0 auto;
	margin-top: 30px;
	color: #B73800;
	font-weight: 600;
	font-size: 18px;
}



</style>
